import { Controller, Get, Query, Post, Body, Request } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { UserService } from '../services/user.service';
import { AuthService } from '../../auth/services/auth.service';
import { User } from '../entities/user.entity';
import { CreateUserDto } from '../dto/create-user.dto';
import { UpdateUserDto } from '../dto/update-user.dto';
import { DeleteUserDto, BatchDeleteUserDto } from '../dto/delete-user.dto';
import { QueryUserDto } from '../dto/query-user.dto';
import { PaginationDto } from '../../../common/dto/pagination.dto';
import { ApiResponseDto } from '../../../common/dto/response.dto';
import { ResponseCode } from '../../../common/types/response-code.enum';
import { Public } from '../../../common/decorators/roles.decorator';
import { CurrentUser } from '../../../common/decorators/current-user.decorator';
import { ApiName } from '../../../common/decorators/api-name.decorator';

@ApiTags('用户管理')
@ApiBearerAuth()
@Controller('users')
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly authService: AuthService,
  ) {}

  

  @Post('create')
  @ApiOperation({ summary: '创建用户' })
  @ApiName('创建用户')
  async createUser(@Body() createUserDto: CreateUserDto): Promise<ApiResponseDto<boolean>> {
    const result = await this.userService.create(createUserDto);
    if (result) {
      return ApiResponseDto.success(true, '用户创建成功');
    } else {
      return ApiResponseDto.error(ResponseCode.BUSINESS_ERROR, '用户创建失败');
    }
  }

  @Post('update')
  @ApiOperation({ summary: '更新用户' })
  @ApiName('更新用户')
  async updateUser(
    @Body() updateUserDto: Partial<UpdateUserDto>,
    @CurrentUser() currentUser: User,
    @Request() req,
  ): Promise<ApiResponseDto<boolean>> {
    const result = await this.userService.update(updateUserDto.id, updateUserDto, currentUser.id);
    if (result) {
      return ApiResponseDto.success(true, '用户更新成功');
    } else {
      return ApiResponseDto.error(ResponseCode.BUSINESS_ERROR, '用户更新失败');
    }
  }

  @Post('delete')
  @ApiOperation({ summary: '删除用户' })
  @ApiName('删除用户')
  async deleteUser(
    @Body() deleteUserDto: DeleteUserDto,
    @CurrentUser() currentUser: User,
    @Request() req,
  ): Promise<ApiResponseDto<boolean>> {
    const result = await this.userService.delete(deleteUserDto.id, currentUser.id);
    if (result) {
      return ApiResponseDto.success(true, '用户删除成功');
    } else {
      return ApiResponseDto.error(ResponseCode.BUSINESS_ERROR, '用户删除失败');
    }
  }

  // @Post('batch-delete')
  // @ApiOperation({ summary: '批量删除用户' })
  // @ApiName('批量删除用户')
  // async batchDeleteUsers(
  //   @Body() batchDeleteDto: BatchDeleteUserDto,
  //   @CurrentUser() currentUser: User,
  //   @Request() req,
  // ): Promise<ApiResponseDto<boolean>> {
  //   const result = await this.userService.batchDelete(batchDeleteDto.ids, currentUser.id);
  //   if (result) {
  //     return ApiResponseDto.success(true, '用户批量删除成功');
  //   } else {
  //     return ApiResponseDto.error(ResponseCode.BUSINESS_ERROR, '用户批量删除失败');
  //   }
  // }

  @Get('page')
  @ApiOperation({ summary: '分页获取用户列表' })
  @ApiName('分页获取用户列表')
  async getUserList(
    @Query() paginationDto: PaginationDto,
  ): Promise<ApiResponseDto<any>> {
    const result = await this.userService.findWithPagination(paginationDto);
    return ApiResponseDto.success(result, '获取用户列表成功');
  }

  @Get('list')
  @ApiOperation({ summary: '获取所有用户' })
  @ApiName('获取所有用户')
  async getAllUsers(@Query() queryDto: QueryUserDto): Promise<ApiResponseDto<User[]>> {
    const users = await this.userService.findAllWithConditions(queryDto);
    return ApiResponseDto.success(users, '获取所有用户成功');
  }

  private getClientIP(req: any): string {
    return (
      req.headers['x-forwarded-for'] ||
      req.headers['x-real-ip'] ||
      req.socket?.remoteAddress ||
      (req.socket?.socket?.remoteAddress 
        ? req.socket.socket.remoteAddress 
        : 'unknown')
    );
  }
} 